<?php
namespace app\admin\logic;
use \PhpOffice\PhpSpreadsheet\Spreadsheet;
use \PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class MatterLogic extends BaseLogic {

    /**
     * 获取事项查询条件
     * @param $from_username
     * @param $keywords
     * @param $status
     * @param $source
     * @param $is_out_time
     * @param $start_time
     * @param $end_time
     * @return array
     */
    public function get_search_matter_maps($from_username,$deal_username,$keywords,$status,$source,$is_out_time,$start_time,$end_time){
        $maps = [];
        $maps[] = ['status','>',0];

        if(!empty($from_username)){
            $u_maps = [];
            $u_maps[] = ['username','like','%' . $from_username . '%'];
            $openids = model('user')->get_column($u_maps,'openid');
            $maps[] = ['from_openid','in',$openids];
        }

        if(!empty($deal_username)){
            $u_maps = [];
            $u_maps[] = ['username','like','%' . $deal_username . '%'];
            $openids = model('user')->get_column($u_maps,'openid');
            $maps[] = ['deal_openid','in',$openids];
        }

        if(!empty($keywords)){
            $maps[] = ['name','like','%' . $keywords . '%'];
        }

        if($status != -1){
            $maps[] = ['status','=',$status];
        }

        if($source != -1){
            $maps[] = ['source','=',$source];
        }

        if($is_out_time != -1){
            $now_time = date('Y-m-d H:i:s');
            
            $matterid_arr = [];

            $m_maps = [];
            $m_list = model('matter')->get_list($m_maps,'id,out_time,status');
            foreach($m_list as $k => $v){
                $mf_maps = [];
                $mf_maps[] = ['matterid','=',$v['id']];
                $mf_maps[] = ['action','=',101];
                $mf_detail = model('matter_flow')->get_detail($mf_maps,'id,c_time');

                if($v['status'] == 1 && $is_out_time == 0){
                    if(!empty($mf_detail)){
                        if($v['out_time'] > $mf_detail['c_time']){
                            $matterid_arr[] = $v['id'];
                        }
                    }
                }elseif($is_out_time == 0){
                    if($v['out_time'] > $now_time){
                        $matterid_arr[] = $v['id'];
                    }
                }elseif($v['status'] == 1 && $is_out_time == 1){
                    if(!empty($mf_detail)){
                        if($v['out_time'] <= $mf_detail['c_time']){
                            $matterid_arr[] = $v['id'];
                        }
                    }
                }elseif($is_out_time == 1){
                    if($v['out_time'] <= $now_time){
                        $matterid_arr[] = $v['id'];
                    }
                }
            }
            $maps[] = ['id','in',$matterid_arr];
        }

        if(!empty($start_time) && !empty($end_time)){
            $maps[] = ['c_time','between',[$start_time,$end_time]];
        }

        return $maps;
    }

    /**
     * 获取事项
     * @param $params
     * @return mixed
     */
    public function get_matter($params){
        $s = new \app\common\controller\Source();

        $maps = $this->get_search_matter_maps($params['from_username'],$params['deal_username'],$params['keywords'],$params['status'],$params['source'],$params['is_out_time'],$params['start_time'],$params['end_time']);
        $limit = ($params['page'] - 1) * $params['rows'];
        $list = model('matter')->get_list($maps,'id,from_openid,sourceid,source,name,out_time,deal_openid,status,c_time','id desc','',$limit,$params['rows']);
        foreach($list as $k => $v){
            $list[$k]['source_zw'] = $v->source_name;
            $list[$k]['status_zw'] = $v->status_name;
            $list[$k]['out_time_zw'] = $v->out_time_name;

            //查找发起人
            $u_maps = [];
            $u_maps[] = ['openid','=',$v['from_openid']];
            $from_u_detail = model('user')->get_detail($u_maps,'id,username');
            $list[$k]['from_username'] = $from_u_detail['username'];

            //查找处理人
            $u_maps = [];
            $u_maps[] = ['openid','=',$v['deal_openid']];
            $deal_u_detail = model('user')->get_detail($u_maps,'id,username');
            $list[$k]['deal_username'] = $deal_u_detail['username'];

            //获取来源信息
            $r = $s->get_source($v['sourceid'],$v['source']);
            $list[$k]['source_info'] = $r['source_info'];

            $flag = 'sx_' . $v['sourceid'] . '_' . $v['source'] . '_';
            $list[$k]['flag'] = $flag;
        }

        $total_num = model('matter')->get_count($maps,'id');

        $data['list'] = $list;
        $data['total_num'] = $total_num;
        return $this->get_response(1,'OK',$data);
    }

    /**
     * 生成事项excel
     * @param $ExcelName
     * @param $data
     * @return bool
     */
    public function gen_matter_excel($ExcelName,$data){
        if(count($data[0]) > 26){
            return $this->get_response(0,'导出excel最大列数为26，请缩减数据列数');
        }
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();
        for($row = 1;$row <= count($data);$row++){
            $index = 0;
            foreach($data[$row-1] as $k => $v) {
                $col = chr($index + 65);
                $cell = ($col . $row);
                $sheet->setCellValue($cell, $v);
                $index++;
            }
        }

        header('Content-Type: application/vnd.ms-excel;charset=utf8-general-ci');
        header('Content-Disposition: attachment;filename='.$ExcelName);
        header('Cache-Control: max-age=0');

        $writer = new Xlsx($spreadsheet);
        $writer->save('php://output');
        //$writer->save($ExcelName);
        return true;
    }
}